// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2). // Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management, // session persistence, api calls, and more. const Alexa = require('ask-sdk-core'); const Util = require('./util'); const LaunchRequestHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; }, handle(handlerInput) { const speakOutput = 'Hi Ashok, Welcome to AC Bot'; return handlerInput.responseBuilder .speak(speakOutput) .reprompt(speakOutput) .getResponse(); } }; const TriggerSwingONIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerSwingON'; }, handle(handlerInput) { const speakOutput = 'Swing is turned ON'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; Util.setvalue('7'); // mqtt_publish("LED ON"); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerSwingOFFIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerSwingOFF'; }, handle(handlerInput) { const speakOutput = 'Swing is turned OFF'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; // mqtt_publish("LED OFF"); Util.setvalue('8'); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerCoolONIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerCoolON'; }, handle(handlerInput) { const speakOutput = 'Cooling is turned ON'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; Util.setvalue('5'); // mqtt_publish("LED ON"); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerCoolOFFIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerCoolOFF'; }, handle(handlerInput) { const speakOutput = 'Cooling is turned OFF'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; // mqtt_publish("LED OFF"); Util.setvalue('6'); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerFanONIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerFanON'; }, handle(handlerInput) { const speakOutput = 'Fan is turned ON'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; Util.setvalue('3'); // mqtt_publish("LED ON"); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerFanOFFIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerFanOFF'; }, handle(handlerInput) { const speakOutput = 'Fan is turned OFF'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; // mqtt_publish("LED OFF"); Util.setvalue('4'); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerPowerONIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerPowerON'; }, handle(handlerInput) { const speakOutput = 'Power is turned ON'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; Util.setvalue('1'); // mqtt_publish("LED ON"); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const TriggerPowerOFFIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'TriggerPowerOFF'; }, handle(handlerInput) { const speakOutput = 'Power is turned OFF'; const attributesManager = handlerInput.attributesManager; // const endpointId = attributesManager.getSessionAttributes().endpointId || []; // mqtt_publish("LED OFF"); Util.setvalue('2'); return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; const HelpIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent'; }, handle(handlerInput) { const speakOutput = 'You can say hello to me! How can I help?'; return handlerInput.responseBuilder .speak(speakOutput) .reprompt(speakOutput) .getResponse(); } }; const CancelAndStopIntentHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent' || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent'); }, handle(handlerInput) { const speakOutput = 'Goodbye!'; return handlerInput.responseBuilder .speak(speakOutput) .getResponse(); } }; const SessionEndedRequestHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest'; }, handle(handlerInput) { // Any cleanup logic goes here. return handlerInput.responseBuilder.getResponse(); } }; // The intent reflector is used for interaction model testing and debugging. // It will simply repeat the intent the user said. You can create custom handlers // for your intents by defining them above, then also adding them to the request // handler chain below. const IntentReflectorHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'; }, handle(handlerInput) { const intentName = Alexa.getIntentName(handlerInput.requestEnvelope); const speakOutput = `You just triggered ${intentName}`; return handlerInput.responseBuilder .speak(speakOutput) //.reprompt('add a reprompt if you want to keep the session open for the user to respond') .getResponse(); } }; // Generic error handling to capture any syntax or routing errors. If you receive an error // stating the request handler chain is not found, you have not implemented a handler for // the intent being invoked or included it in the skill builder below. const ErrorHandler = { canHandle() { return true; }, handle(handlerInput, error) { console.log(`~~~~ Error handled: ${error.stack}`); const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`; return handlerInput.responseBuilder .speak(speakOutput) .reprompt(speakOutput) .getResponse(); } }; // The SkillBuilder acts as the entry point for your skill, routing all request and response // payloads to the handlers above. Make sure any new handlers or interceptors you've // defined are included below. The order matters - they're processed top to bottom. exports.handler = Alexa.SkillBuilders.custom() .addRequestHandlers( LaunchRequestHandler, TriggerPowerONIntentHandler, TriggerPowerOFFIntentHandler, TriggerFanONIntentHandler, TriggerFanOFFIntentHandler, TriggerCoolONIntentHandler, TriggerCoolOFFIntentHandler, TriggerSwingONIntentHandler, TriggerSwingOFFIntentHandler, HelpIntentHandler, CancelAndStopIntentHandler, SessionEndedRequestHandler, IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers ) .addErrorHandlers( ErrorHandler, ) .lambda();